package cn.trip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripComment;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.*;
import cn.itrip.common.Page;
import cn.itrip.common.RedisUtil;
import cn.trip.biz.service.ItripCommentService;
import cn.trip.biz.service.ItripHotelDicService;
import cn.trip.biz.service.ItripHotelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@Api(value = "comment|评价的控制器")
@RequestMapping("/api/comment")
public class HotelCommentController {
    @Autowired
    private ItripHotelDicService itripHotelDicService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ItripHotelService itripHotelService;
    @Autowired
    private ItripCommentService itripCommentService;

    /**
     * 获取酒店信息
     *
     * @param hotelId
     * @param request
     * @return
     */
    @ApiOperation(value = "获取酒店信息", httpMethod = "GET", produces = "application/json",
            notes = "获取酒店信息")
    @RequestMapping(value = "/gethoteldesc/{hotelId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<Map<String, Object>> getHotelDesc(@PathVariable String hotelId, HttpServletRequest request) {
        Dto<Map<String, Object>> dto = null;
        String token = request.getHeader("token");
        if (redisUtil.hasKey(token)) {
            dto = itripHotelService.selectHotelByid(Long.valueOf(hotelId));
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token已失效");
        }
        return dto;
    }

    /**
     * 获取出游类型
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取出游类型", httpMethod = "GET", produces = "application/json",
            notes = "获取出游类型：success = ‘true’ | 失败：success = ‘false’ ")
    @RequestMapping(value = "/gettraveltype", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<List<ItripLabelDicVO>> gettraveltype(HttpServletRequest request) {
        Dto<List<ItripLabelDicVO>> dto = null;
        String token = request.getHeader("token");
        if (redisUtil.hasKey(token)) {
            //旅游的parentId=107;
            dto = itripHotelDicService.selectTravelType(107L);
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token已失效");
        }
        return dto;
    }

    /**
     * 上传图片到虚拟机linux下/data/itrip/images/comment
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "上传图片", produces = "application/json", notes = "酒店点评内上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto<Object> upload(HttpServletRequest request) {
        Dto<Object> dto = null;
        if (redisUtil.hasKey(request.getHeader("token"))) {
            dto = itripCommentService.uploadFile(request);
        } else {
            dto = new Dto<>();
            dto.setSuccess("false");
            dto.setMsg("token已过期");
        }
        return dto;
    }

    /**
     * 增加评价
     *
     * @param itripCommentAddVO
     * @param request
     * @return
     */
    @ApiOperation(value = "增加评论", produces = "application/json", notes = "酒店点评保存")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto add(@RequestBody ItripCommentAddVO itripCommentAddVO, HttpServletRequest request) {
        Dto dto = null;
        String token = request.getHeader("token");
        if (redisUtil.hasKey(token)) {
            ItripUser user = (ItripUser) redisUtil.get(token);
            Map<String, Object> map = new HashMap<>();
            map.put("itripUser", user);
            map.put("itripCommentAddVO", itripCommentAddVO);
            map.put("score", (itripCommentAddVO.getFacilitiesScore() + itripCommentAddVO.getHygieneScore() + itripCommentAddVO.getPositionScore() + itripCommentAddVO.getServiceScore()) / 4);
            dto = itripCommentService.doAddComment(map);

        } else {
            dto = new Dto<>();
            dto.setSuccess("false");
            dto.setMsg("token已过期");
        }
        return dto;
    }

    /**
     * 获取评论列表
     *
     * @param itripComment
     * @return
     */
    @ApiOperation(value = "获取评论列表", produces = "application/json", notes = "评论列表")
    @RequestMapping(value = "/getcommentlist", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto<Page<ItripListCommentVO>> getcommentlist(@RequestBody ItripComment itripComment) {
        Dto<Page<ItripListCommentVO>> dto = null;
        itripComment.setPageNo((itripComment.getPageNo() - 1) * itripComment.getPageSize());
        dto = itripCommentService.getCommentList(itripComment);
        return dto;
    }

    /**
     * 获取评论总数
     *
     * @return
     */
    @ApiOperation(value = "获取总数", produces = "application/json", notes = "评论列表")
    @RequestMapping(value = "/getcount/{hotelId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto getcount(@PathVariable String hotelId) {
        Dto dto = null;
        ItripComment itripComment = new ItripComment();
        itripComment.setHotelId(new Long(hotelId));
        dto = itripCommentService.count(itripComment);
        return dto;
    }

    /**
     * 获取图片
     *
     * @return
     */
    @ApiOperation(value = "获取图片", produces = "application/json", notes = "评论列表")
    @RequestMapping(value = "/getimg/{targetId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<List<ItripImageVO>> getimg(@PathVariable String targetId) {
        Dto<List<ItripImageVO>> dto = null;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("type", "2");
        param.put("targetId", new Long(targetId));
        dto = itripHotelService.getItripImage(param);
        return dto;
    }

    /**
     * 获取评分
     *
     * @return
     */
    @ApiOperation(value = "获取评分", produces = "application/json", notes = "评论列表")
    @RequestMapping(value = "/gethotelscore/{hotelId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto gethotelscore(@PathVariable String hotelId) {
        Dto dto = itripCommentService.getScoreByHotelId(new Long(hotelId));
        return dto;
    }


}
